from socket import timeout
import time
import RPi.GPIO as GPIO
import sys
import os
import threading
import random

last_pwm_x=7.5
last_pwm_y=7.5
pwm_x_min=5.5
pwm_x_max=10.5
pwm_y_min=7.0
pwm_y_max=9.0
file_name_x="config_x.txt"
file_name_y="config_y.txt"
p_gpio_x=7
p_gpio_y=18


def save_to_file_x(data):
    try:
        global file_name_x
        file=open(file_name_x,'w+')
        file.read()
        file.write(data)
        file.flush()
        file.close()
    finally:
        if file:
            file.close()
    return
def save_to_file_y(data):
    try:
        global file_name_y
        file=open(file_name_y,'w+')
        file.read()
        file.write(data)
        file.flush()
        file.close()
    finally:
        if file:
            file.close()
    return
def init_pwm_set():
    global last_pwm_x,last_pwm_y,file_name_x,file_name_y
    try:
        if os.path.exists(file_name_x):
            file_x=open(file_name_x,'r')
            pwm_x=file_x.readline()
            last_pwm_x=float(pwm_x)
            file_x.close()
        if os.path.exists(file_name_y):
            file_y=open(file_name_y,'r')
            pwm_y=file_y.readline()
            last_pwm_y=float(pwm_y)
            file_y.close()
    except Exception as e:
        print(e)
        file_x.close()
        file_y.close()
    return

def send_pwm_x(target_pwm):
    global last_pwm_x,p_gpio_x
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BOARD)
    print('gpio',p_gpio_x,last_pwm_x,target_pwm,threading.current_thread().name)
    GPIO.setup(p_gpio_x,GPIO.OUT,initial=GPIO.LOW)
    pwm=GPIO.PWM(p_gpio_x,50)
    pwm.start(0)
    if last_pwm_x<target_pwm:
        while last_pwm_x<=target_pwm:
            pwm.ChangeDutyCycle(last_pwm_x)
            print('pwm',p_gpio_x,last_pwm_x)
            last_pwm_x=float(format(last_pwm_x+0.005,'.3f'))
            time.sleep(0.005)
    else:
        while last_pwm_x>=target_pwm:
            pwm.ChangeDutyCycle(last_pwm_x)
            print('pwm',p_gpio_x,last_pwm_x)
            last_pwm_x=float(format(last_pwm_x-0.005,'.3f'))
            time.sleep(0.005)
    time.sleep(0.02)
    pwm.stop()
    GPIO.cleanup(p_gpio_x)
    save_to_file_x(str(last_pwm_x))
    return

def send_pwm_y(target_pwm):
    global last_pwm_y,p_gpio_y
    print('gpio',p_gpio_y,last_pwm_y,target_pwm,threading.current_thread().name)
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(p_gpio_y,GPIO.OUT,initial=GPIO.LOW)
    pwm_y=GPIO.PWM(p_gpio_y,50)   
    pwm_y.start(0)
    if last_pwm_y<target_pwm:
        while last_pwm_y<=target_pwm:
            pwm_y.ChangeDutyCycle(last_pwm_y)
            print('pwm',p_gpio_y,last_pwm_y)
            last_pwm_y=float(format(last_pwm_y+0.005,'.3f'))
            time.sleep(0.005)
    else:
        while last_pwm_y>=target_pwm:
            pwm_y.ChangeDutyCycle(last_pwm_y)
            print('pwm',p_gpio_y,last_pwm_y)
            last_pwm_y=float(format(last_pwm_y-0.005,'.3f'))
            time.sleep(0.005)
    time.sleep(0.02)
    pwm_y.stop()
    GPIO.cleanup(p_gpio_y)
    save_to_file_y(str(last_pwm_y))
    return

def main():
    try:
        init_pwm_set()
        while True:
            target_pwm_x=random.uniform(pwm_x_min,pwm_x_max)
            thread_pwm_x=threading.Thread(target=send_pwm_x,args=(target_pwm_x,),name="thread_pwm_x")
            thread_pwm_x.start()
            target_pwm_y=random.uniform(pwm_y_min,pwm_y_max)
            thread_pwm_y=threading.Thread(target=send_pwm_y,args=(target_pwm_y,),name="thread_pwm_y")
            thread_pwm_y.start()
            print(target_pwm_x,target_pwm_y)
            time.sleep(5)
    except Exception or KeyboardInterrupt as e:
        print('error',e)
main()